﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.UICommon;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainCustomerAssignment : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        #endregion

        #region Constructor
        public frmMaintainCustomerAssignment(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }
        #endregion

        #region Functions

        private void BingCombobox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colLevel, "CL", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCurrencyType, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "STATUS", true);
        }

        private void BindDataGrid()
        {
            DataTable dt = MstCustomerBs.GetCustomerByCondition("", "INDUSTRY_TYPE_CODE", "");
            dt.Columns.Add("IS_CHECKED",typeof(Boolean));
            gvMain.DataSource = dt;
        }

        private void SetDataGrid()
        {
            if (mSelectedPerson == null)
            {
                return;
            }
            List<MstPersonCustomerEntity> list = MstPersonCustomerBs.GetByPersonID(mSelectedPerson.PersonId);
            List<string> listCustomer = new List<string>();
            foreach (MstPersonCustomerEntity entity in list)
            {
                listCustomer.Add(entity.CustomerId);
            }
            DataTable dt = gvMain.DataSource as DataTable;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                MstCustomerEntity entity = new MstCustomerEntity(dt.Rows[i]);
                if (listCustomer.Contains(entity.CustomerId))
                {
                    gridView1.SetRowCellValue(i, gridColumn1, true);
                }
                else
                {
                    gridView1.SetRowCellValue(i, gridColumn1, false);
                }
            }
        }

        private bool Save()
        {
            try
            {
                List<MstPersonCustomerEntity> list = new List<MstPersonCustomerEntity>();
                DataTable dt = gvMain.DataSource as DataTable;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (gridView1.GetRowCellValue(i,gridColumn1)!=null)
                    {
                        if (Convert.ToBoolean(gridView1.GetRowCellValue(i, gridColumn1)))
                        {                            
                            //MstCustomerEntity entity = new MstCustomerEntity(dt.Rows[i]);
                            //if (MstPersonCustomerBs.CheckCustomerPerson(entity.CustomerId, mSelectedPerson.PersonId) == false)
                            //{
                            //    MessageBox.Show("客户‘" + entity.CustomerName + "’已经分配给别的客户，请重新选择！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //    return false;
                            //}
                            MstPersonCustomerEntity personCustomer = new MstPersonCustomerEntity();
                            personCustomer.PersonId = mSelectedPerson.PersonId;
                            personCustomer.CustomerId = dt.Rows[i]["CUSTOMER_ID"].ToString();
                            list.Add(personCustomer);
                        }
                    }
                }
                return MstPersonCustomerBs.SavePersonCustomers(mSelectedPerson, list);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        private void frmMaintainCustomerAssignment_Load(object sender, EventArgs e)
        {
            if (mSelectedPerson == null)
            {
                return;
            }
            txtPersonName.Text = mSelectedPerson.NameCn;
            BingCombobox();
            BindDataGrid();
            SetDataGrid();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        #endregion

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            if (Convert.ToBoolean(dr["IS_CHECKED"].ToString()) == false)
            {
                dr["IS_CHECKED"] = true;
            }
            else
            {
                dr["IS_CHECKED"] = false;
            }
        }
    }
}
